#include "Item.h"
#include "Config.h"
#include "MathUtil.h"


Item::Item(void)
{
	_id = NULL;
	_processor = NULL;
	_timeMagnet = 0;
}


Item::~Item(void)
{
	CC_SAFE_RELEASE(_id);
	CC_SAFE_RELEASE(_processor);
}

bool Item::init()
{
	_acceleration = VECTOR_ACCELERATION;
	int x = MathUtil::randonInt(ITEM_OUT_X_MIN, ITEM_OUT_X_MAX);
	int y = MathUtil::randonInt(ITEM_OUT_Y_MIN, ITEM_OUT_Y_MAX);
	_velocity = ccp(x, y);
	return true;
}

void Item::updatePosition( float dt )
{
	_timeMagnet += dt;
	setPositionX(getPositionX() + _velocity.x * dt + 1/2.0f * _acceleration.x * dt * dt);
	setPositionY(getPositionY() + _velocity.y * dt + 1/2.0f * _acceleration.y * dt * dt);
	float velX = _velocity.x + _acceleration.x * dt;
	float velY = _velocity.y + _acceleration.y * dt;
	setVelocity(ccp(velX, velY));
	
	// 
}

ItemProcessor* Item::getProcessor()
{
	return _processor;
}

void Item::setProcessor(ItemProcessor* var)
{
	if (var)
	{
		CC_SAFE_RELEASE(_processor);
		CC_SAFE_RETAIN(var);
		_processor = var;
		_processor->setId(_id);
	}
}